﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;

//------------------------------------------------------
//クラス：H3A1 07
//名　前：仮屋　智央
//内　容：前画面で選択した科目に対応するクラスを表示し
//　　　：その中から出欠を取るクラスを選択する画面
//------------------------------------------------------

namespace h3a1_felica2
{
    public partial class Form3 : Form
    {
        string str1, str2;
        int class_subject_id;
        Form2 f2;
        ArrayList array = new ArrayList();

        public Form3(string str1, string str2,Form2 form2)
        {
            InitializeComponent();

            f2 = form2;
            this.KeyDown += new KeyEventHandler(Form3_KeyDown);

            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.Text = "クラス選択";
            this.button1.Text = "次へ";
            this.button2.Text = "戻る";

            this.str1 = str1; //科目名
            this.str2 = str2; //時限目

            this.label1.Text = Parameter.lecturer_name;
            this.FormClosing += new FormClosingEventHandler(Form3_FormClosing);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            string uri = System.Configuration.ConfigurationSettings.AppSettings["uri"];
            string database = System.Configuration.ConfigurationSettings.AppSettings["database"];
            try
            {

                MySqlConnection cn;
                MySqlCommand cmd;
                MySqlDataReader reader;
                cn = new MySqlConnection();
                cn.ConnectionString = "Server="+uri+";Uid=root;Pwd=mysql;database=" + database + ";";
                cmd = new MySqlCommand();
                cmd.Connection = cn;
                cn.Open();

                //科目名のＩＤを取得
                cmd.CommandText = "select class_subject_id from class_subject where class_subject_name ='" + str1 + "';";
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Parameter.class_sublect_id = int.Parse(reader[0].ToString()); //科目ＩＤを取得
                }
                reader.Close();

                //選択した科目から対象のクラスを取得
                cmd.CommandText = "select class.class_id,class.class_name from class inner join correspondance_courses on class.class_id = correspondance_courses.class_id where correspondance_courses.class_subject_id =" + Parameter.class_sublect_id + ";";
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    checkedListBox1.Items.Add(reader[1].ToString()); //class_nameをリストボックスに追加
                }
                reader.Close();

                cn.Close();
            }
            catch (MySqlException e1)
            {
                MessageBox.Show(e1.Message);
            }
        }

        //次へボタンを押した時
        private void button1_Click(object sender, EventArgs e)
        {

            int count = checkedListBox1.CheckedItems.Count; //対象のクラスが何クラスあるか取得
            string[] selected_param = new string[count]; //対象のクラス数ぶんだけ配列を生成
            for (int i = 0; i < count; i++)
            {
                Parameter.selected_class.Add(checkedListBox1.CheckedItems[i].ToString());
            }

            //クラス名が選択されたらForm4を呼び出す
            if (selected_param.Length > 0)
            {
                //座標の取得
                int map_x = this.Location.X;
                int map_y = this.Location.Y;

                Form4 form4 = new Form4();
                form4.StartPosition = FormStartPosition.Manual;
                form4.Location = new Point(map_x, map_y);
                this.Hide();
                form4.ShowDialog();
                this.Show();
                
            }
        }

        // FormClosing イベントのイベントハンドラ
        private void Form3_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        
        //KeyDownのイベントハンドラ
        void Form3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                f2.Show();
                this.Hide();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //座標の取得
            int map_x = this.Location.X;
            int map_y = this.Location.Y;

            //this.Close();
            Form2 form2 = new Form2();
            form2.StartPosition = FormStartPosition.Manual;
            form2.Location = new Point(map_x, map_y);
            form2.Show();
            form2.TopMost = true;
            this.Hide();
            
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
